package com.nuanshui.heatedloan.credit.pay;

import com.aipg.common.AipgReq;
import com.aipg.common.AipgRsp;
import com.aipg.qtd.QTDRspDetail;
import com.aipg.rtrsp.TransRet;
import com.aipg.transquery.QTDetail;
import com.nuanshui.heatedloan.credit.pay.constant.OrderPayState;
import com.nuanshui.heatedloan.credit.pay.utils.AllinPayUtil;
import com.nuanshui.heatedloan.credit.pay.validation.ValidationResult;
import com.nuanshui.heatedloan.credit.pay.validation.ValidationResultCode;

public class PayContext {

    private static final ThreadLocal<PayContext> LOCAL = new ThreadLocal<PayContext>() {
        @Override
        protected PayContext initialValue() {
            return new PayContext();
        }
    };

    /**
     * get context.
     *
     * @return context
     */
    public static PayContext getContext() {
        return LOCAL.get();
    }

    /**
     * remove context.
     *
     */
    public static void removeContext() {
        LOCAL.remove();
    }


    public AipgRsp getAipgRsp() {
        return aipgRsp;
    }

    public void setAipgRsp(AipgRsp aipgRsp) {
        this.aipgRsp = aipgRsp;
    }

    private AipgRsp aipgRsp;


    public PayRequest getPayRequest() {
        return payRequest;
    }

    public void setPayRequest(PayRequest payRequest) {
        this.payRequest = payRequest;
    }

    private PayRequest payRequest;


    public AipgReq getAipgReq() {
        return aipgReq;
    }

    public void setAipgReq(AipgReq aipgReq) {
        this.aipgReq = aipgReq;
    }

    private AipgReq aipgReq;


    public boolean isSuccess()
    {
        boolean bFlag = false;

        if(getAllinPayResultNotify()!=null)
        {
            if(getAllinPayResultNotify().getBIZTYPE().equals(AllinPayResultNotify.BIZTYPE_SUUCESS))
            {
                return true;
            }
            return false;
        }

        if (AllinPayResultCode.isSuccess(aipgRsp)) {


            if (TransRequestCode.isPayCode(this.getPayRequest().getTransRequestCode())) {
                TransRet transRet = AllinPayUtil.getTrxDataItem(aipgRsp);

                if (transRet != null) {
                    if (AllinPayResultCode.isSuccess(transRet.getRET_CODE())) {
                        bFlag = true;
                    }
                }
            } else
            {
                bFlag=true;
            }
        }

        return bFlag;
    }

    private PayResult payResult=new PayResult();

    public PayResult getPayResult()
    {
        return payResult;
    }

    public PayResult fetchPayResult()
    {
        if(validationResult==null || ValidationResultCode.isSuccess(validationResult.getCode()))
        {
            payResult.setCode(OrderPayState.STATE_SUCCESS);
            payResult.setMsg("处理成功");
        }
        else if(ValidationResultCode.VALIDATION_ALLINPAY_PROCESSING.getCode().equals(validationResult.getCode()))
        {
            payResult.setCode(OrderPayState.STATE_PROCESSING);
            payResult.setMsg("处理中");
        }
        else if(ValidationResultCode.VALIDATION_ERROR_ALLINPAY_TRADE.getCode().equals(validationResult.getCode()))
        {
            payResult.setCode(OrderPayState.STATE_FAIL);
            payResult.setErrorCode(validationResult.getCode());

            if(aipgRsp!=null)
            {
                payResult.setAllinPayCode(aipgRsp.getINFO().getRET_CODE());
                payResult.setMsg(aipgRsp.getINFO().getERR_MSG());
            }
        }
        else
        {
            payResult.setCode(OrderPayState.STATE_FAIL);
            payResult.setErrorCode(validationResult.getCode());
            payResult.setMsg(validationResult.getMsg());
        }

        return payResult;
    }

    private ValidationResult validationResult;

    public ValidationResult getValidationResult() {
        return validationResult;
    }

    public void setValidationResult(ValidationResult validationResult) {
        this.validationResult = validationResult;
    }

    public boolean hasError()
    {
        return validationResult!=null && (!this.validationResult.getCode().equals(ValidationResultCode.VALIDATION_SUCCESS.getCode()));
    }

    private Object data;

    public <T> T getData() {
        return (T)data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public boolean isFromNotify()
    {
        return (allinPayResultNotify !=null);
    }

    private AllinPayResultNotify allinPayResultNotify;

    public AllinPayResultNotify getAllinPayResultNotify() {
        return allinPayResultNotify;
    }

    public void setAllinPayResultNotify(AllinPayResultNotify allinPayResultNotify) {
        this.allinPayResultNotify = allinPayResultNotify;
    }

    public String getPayNum() {
        String reqSn = null;
        if (getAipgReq() != null) {
            reqSn = getAipgReq().getINFO().getREQ_SN();
        }
        else if (getAllinPayResultNotify() != null) {
            reqSn = getAllinPayResultNotify().getBATCHID();
        }
        return reqSn;
    }

    public String getRetCode() {
        String retCode = null;
        if (getAipgRsp() != null) {
            retCode = getAipgRsp().getINFO().getRET_CODE();
        }
        else if (getAllinPayResultNotify() != null) {
            retCode = getAllinPayResultNotify().getRETCODE();
        }
        return retCode;
    }

    private AllinPayResponse tradeDetail;

    public AllinPayResponse getTradeDetail() {
        return tradeDetail;
    }

    public void setTradeDetail(AllinPayResponse tradeDetail) {
        this.tradeDetail = tradeDetail;
    }
}
